package com.example.androidproject.novel.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.androidproject.app.ClassifyActivity3;
import com.example.androidproject.novel.bean.NovelBean;
import com.example.androidproject.R;

import java.util.ArrayList;

public class RecommendAdapter extends BaseAdapter {
    private final String TAG = "GGSTUDY";
    private final Context mContext;
    private final ArrayList<NovelBean.DataBean> classicNewBook;
    private final ArrayList<NovelBean.DataBean> classicHotBody;
    private final ArrayList<NovelBean.DataBean> hotRecommentBody;
    private setOnItemClickListener clickListener;

    public RecommendAdapter(Context mContext, ArrayList<NovelBean.DataBean> classicNewBody, ArrayList<NovelBean.DataBean> classicHotBody, ArrayList<NovelBean.DataBean> hotRecommentBody) {
        this.mContext = mContext;
        this.classicNewBook = classicNewBody;
        this.classicHotBody = classicHotBody;
        this.hotRecommentBody = hotRecommentBody;
    }


    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Object getItem(int position) {
        return classicNewBook.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        ViewHolder viewHolder;
        View view = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = mInflater.inflate(R.layout.recomment_item, null);
            viewHolder.gv_recomment = view.findViewById(R.id.gv_recomment);
            viewHolder.tv_Recomment = view.findViewById(R.id.tv_Recomment);
            viewHolder.ll_more = view.findViewById(R.id.ll_more);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (position == 0) {
            viewHolder.tv_Recomment.setText("经典新书");
            RecommendContentAdapter adpater = new RecommendContentAdapter(mContext, classicNewBook);
            viewHolder.gv_recomment.setAdapter(adpater);
        }
        if (position == 1) {
            viewHolder.tv_Recomment.setText("经典热书");
            RecommendContentAdapter adpater1 = new RecommendContentAdapter(mContext, classicHotBody);
            viewHolder.gv_recomment.setAdapter(adpater1);
        }
        if (position == 2) {
            viewHolder.tv_Recomment.setText("热门推荐");
            RecommendContentAdapter adpater2 = new RecommendContentAdapter(mContext, hotRecommentBody);
            viewHolder.gv_recomment.setAdapter(adpater2);
        }
        viewHolder.ll_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ClassifyActivity3.class);
                Bundle bundle = new Bundle();
                bundle.putInt("flag", 2);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        viewHolder.gv_recomment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                clickListener.onItemClick(parent, view, position, id);
            }
        });
        return view;
    }

    class ViewHolder {
        TextView tv_Recomment;
        GridView gv_recomment;
        LinearLayout ll_more;
    }

    public interface setOnItemClickListener {
        void onItemClick(AdapterView<?> parent, View view, int position, long id);
    }

    public void setOnItemClickListener(setOnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }
}
